package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

public class Planet extends AbstractSolarSystemObject {
    public int longAxis;
    public int shortAxis;
    public double  speed;
    public double theta;
    public Sun sun ;
    public String name;
    public boolean smallPlanet;
    public Planet(){}

    public Planet(Sun sun,String imageKey,double AU,double e,int T,String Name){
        this.sun=sun;
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxisBYAU(AU);
        this.shortAxis = getShortAxisBYE(e);
        this.x = Constant.WINDOW_WIDTH/2+longAxis;
        this.y = Constant.WINDOW_HEIGHT/2;
        this.speed = getSpeedByT(T);
        this.theta = 0.0;
        this.name=Name;
    }
    public Planet(Sun sun,String imageName,double AU,double e,int T,String Name,boolean smallPlanet){
        this(sun,imageName,AU,e,T,Name);
        this.smallPlanet = smallPlanet;
    }
    private int getLongAxisBYAU(double AU){
        return (int)(100*AU);
    }
    private  int getShortAxisBYE(double e ){
        return (int)(this.longAxis * Math.sqrt(1 - e * e))/5;
    }
    private double getSpeedByT(int t){
        return 0.1 * 365 / t;

    }


    @Override
    public void draw(Graphics g) {

        Color c =g.getColor();
        g.setColor(Color.white);
        if(!smallPlanet){
            drawTrace(g);
        }
        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,24));
        g.drawString(name,x,y);
        g.setColor(c);
        g.setFont(f);


        g.drawImage(img,x,y,null);//画图片
        move();//动

    }
    private void drawTrace(Graphics g){

        int x =this.sun.x-this.longAxis + sun.width/2;
        int y = this.sun.y-this.shortAxis + sun.height/2;
        int width = 2 * this.longAxis;
        int height = 2* this.shortAxis;
        g.drawOval(x,y,width,height);

    }

    @Override
    public void move() {
        x = (int) (longAxis * Math.cos(theta)) + sun.x + sun.width/2-this.width/2;
        y = (int) (shortAxis*Math.sin(theta))+ sun.y + sun.height/2-this.height/2;
        theta+=speed;

    }
}
